package org.example.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import org.example.common.JWTHS256;
import org.example.entity.UserEntity;
import org.example.service.UserService;
import org.example.common.Result;
import org.springframework.dao.DataAccessException;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(origins = "*",maxAge = 3600)
public class UserController {
    @Resource
    private UserService userService;

    /*
    用户登录
     */
    @RequestMapping(value = "login",method = RequestMethod.POST)
    public String login(@RequestBody Map<String,Object> params){

        Map<String,Object> data = new HashMap<>();
        Result result= new Result();
        String schoolid = params.get("schoolid").toString();
        String password = params.get("password").toString();

        if(schoolid == null ||password==null){
            data.put("token",null);
            result.setStatus(201);
            result.setMsg("用户名或密码不能为空");
        }
        else {
            //从数据库中查询用户信息
            //Map<String,Object> map = new HashMap<>();
            UserEntity user = userService.queryBySchoolid(schoolid);
            if(user==null){
                data.put("token",null);
                result.setStatus(203);
                result.setMsg("用户名不存在");
            }else{
                //如果密码正确，返回token信息
                String pw = user.getPassword();
                if (password.equals(pw)) {
                    //HS256加密生成Token
                    String token = JWTHS256.buildJWT(schoolid);
                    data.put("token", token);
                    data.put("userinfo", user);
                    result.setStatus(200);
                    result.setMsg("登录成功");
                }
                else{
                    data.put("token",null);
                    result.setStatus(202);
                    result.setMsg("用户名或密码错误");
                }
            }
        }
        result.setData(data);
        //如果用户验证失败，token为空
        return JSONUtil.toJsonStr(result);
    }

    /*
    用户注册
     */
    @RequestMapping(value = "reg",method = RequestMethod.POST)
    public String register(@RequestBody UserEntity newUser){
        Result result= new Result();

        try{
            userService.insertUser(newUser);
            result.setStatus(200);
            result.setMsg("注册成功");
        }catch (DataAccessException e){
            result.setStatus(201);
            result.setMsg("注册失败,该学工号已注册");
        }

        return JSONUtil.toJsonStr(result);
    }

    /*
    获取用户信息
     */
    @RequestMapping(value = "userinfo",method = RequestMethod.GET)
    public String getUserinfo(@RequestHeader("authorization") String token) {
        String schoolid = JWTHS256.vaildToken(token);
        Result result = new Result();

        UserEntity user = userService.queryBySchoolid(schoolid);
        result.setStatus(200);
        result.setMsg("查询用户信息成功");
        user.setPassword(null);
        result.setData(user);

        return JSONUtil.toJsonStr(result);
    }

    /*
    更新用户信息
     */
    @RequestMapping(value = "updateUser",method = RequestMethod.PUT)
    public String updateUser(@RequestBody UserEntity upUser){
        Result result = new Result();

        //int res = userService.updateUser(upUser);

        result.setStatus(200);
        result.setMsg("查询用户信息成功");

        return JSONUtil.toJsonStr(result);
    }

    /*
    验证token
    */
//    @RequestMapping("vaildToken")
//    public String vaildToken(@RequestHeader Map<String, String> headers){
//        //获取请求头的token信息
//        String token = headers.get("token");
//        //解密验证token
//        String res = JWTHS256.vaildToken(token);
//
//        Result result= new Result(200,"success",res);
//        return JSONUtil.toJsonStr(result);
//    }

}
